﻿using System.Threading.Tasks;
using MQTTnet.Server;
using OperationDataBase;

namespace Service.Mqtt.Callback.Handlers
{
    /// <summary>
    /// 订阅拦截
    /// </summary>
    public class MqttServerSubscriptionInterceptor : IMqttServerSubscriptionInterceptor
    {
        public MqttServerSubscriptionInterceptor()
        {
        }
        public Task InterceptSubscriptionAsync(MqttSubscriptionInterceptorContext context)
        {
            // we do not process messages from ourselves
            if (string.IsNullOrEmpty(context.ClientId))
            {
                return Task.CompletedTask;
            }
            try
            {
                if (string.IsNullOrWhiteSpace(context.TopicFilter.Topic))
                {
                    context.AcceptSubscription = false;
                    context.CloseConnection = true;
                    return Task.CompletedTask;
                }
              
                context.AcceptSubscription = true;
                return Task.CompletedTask;
            }
            catch
            {
                return Task.CompletedTask;
            }
        }

    }
}
